"""Rumour router for Dingo.

Copyright (C) Sarah Mount 2008.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
"""

from __future__ import absolute_import
del absolute_import
from .. import RoutingModel

DEBUG = True
#DEBUG = False

class IdentStamper(object):
    __instance = None       
    def __new__(cls, *args, **kargs): 
        """Create a new instance of this class, ensuring conformance
        to the singleton pattern.

        See U{http://en.wikipedia.org/wiki/Singleton_pattern#Python}
        """
        if cls.__instance is None:
            cls.__instance = object.__new__(cls, *args, **kargs)
        return cls.__instance
    def __init__(self):
        self.ident = 0
    	return
    def getIdent(self):
	self.ident += 1
	return self.ident


class RumorRouter(RoutingModel.RoutingModel):
    """Rumor routing.

    David Braginsky and Deborah Estrin. Rumor routing algorithm for
    sensor networks. In Proceedings of the First ACM Workshop on
    Sensor Networks and Applications, pages 22--31, Atlanta, GA, USA,
    October 2002. ACM.
    """

    HELLOWPKT = 0
    """@cvar: Message type for 'Hello' packets"""
    AGENTPKT = 1
    """@cvar: Message type for 'Agent' packets"""
    QUERYPKT = 2
    """@cvar: Message type for 'Query' packets"""

    def __init__(self, ttl=10):
        RoutingModel.RoutingModel.__init__(self)
        self.name = 'Rumour router'
        self.ttl = ttl # Time to live measured in hops
	self.messages = [] # Messages already forwarded by this router
        return

    def send(self, arc_colour):
        return

    def recv(self):
        return

    def debugRoute(self, msg):
	"""Return string representing route taken by message."""
	r = msg.route.keys()
	r.sort()
	print
	print 'Message %i received by sensor %s with following routing table:' % (msg.ident, self.sensor_id)
	print '--------------------------------------------------------------'
	print 'Intended destination: %s' % msg.dest
	print 'Current hop count: %i' % msg.hopCount
	print '---------------'
	for hop in r:
		print 'Hop %i : sensor %s : time %d' % (hop, msg.route[hop][0], msg.route[hop][1])
	print '---------------'
	print
	return


rumorRouter = RumorRouter()
